const app=getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    bannerlist:[],
    catelist:[],
    recbanner:[],
    newslist:[],
    timer:null,
    timerSeckill:null,
    keyword: '',
    page: 1,
    list: []
  },
  search () {
    if (!this.data.keyword) {
      wx.showToast({
        title: '请输入关键词!',
        icon: 'none'
      })
      return
    }
    wx.navigateTo({
      url: '/pages/shop/shoplist/shoplist?keyword=' + this.data.keyword
    })
  },
  keywordInput (e) {
    let val = e.detail.value
    this.setData({
      keyword: val
    })
  },
  // 首页广告位跳转
  adLink(e) {
    let typeid = e.currentTarget.dataset.typeid;
    let linkid = e.currentTarget.dataset.linkid;
    if (typeid == 1) {
      wx.navigateTo({
        url: "/pages/shop/shopinfo/shopinfo?shopid=" + linkid,
      })
    } else if (typeid == 2) {
      wx.navigateTo({
        url: "/pages/shop/shoplist/shoplist?cid=" + linkid,
      })
    } else if (typeid == 3) {
      wx.navigateTo({
        url: "/pages/news_info/news_info?id=" + linkid,
      })
    }
  },
  getIndex:function(){
    let data={};
    app.requestJs('Index/index',{data}).then((res)=>{
      if(res.errcode==1){
        this.setData({
          bannerlist:res.data.bannerlist || [],
          catelist:res.data.catelist || [],
          recbanner:res.data.recbanner || [],
          newslist:res.data.newslist || [],
          spike_list:res.data.spike_list || [],//抢购
          seckill_list:res.data.seckill_list || [],//限时购
          surplus:res.data.surplus
        })
        if(res.data.seckill_list.length>0)this.buyTime(res.data.surplus.surplustimea);
        if(res.data.spike_list.length>0)this.buySeckill(res.data.surplus.surplustimeb);
      }else app.utilJs.toast({ title: res.message });
    })
  },
  cateLink:function(e){
    let cid=e.currentTarget.dataset.cid || '';
    app.globalData.cateId=cid;
    wx.switchTab({
      url:'/pages/cate/cate',
    })
  },
  activityLink:function(e){
    let type=e.currentTarget.dataset.type;
    wx.navigateTo({
      url: '/pages/shop/shop_activity_list/shop_activity_list?typeid='+type,
    })
  },
  buyTime:function(time){//计时器 限时购
    if(time<=0 || !time)return;
    let timer=this.data.timer;
    let timelimit='';
    timer=setInterval(()=>{
      console.log(this.data.timer)
      if(time<=0){
        clearInterval(this.data.timer);
        this.getIndex();
      }else if(!this.data.timer)clearInterval(this.data.timer);
      else time=time-1;
      timelimit=this.timerCount(time);
      this.setData({
        timelimit
      })
    },1000);
    this.setData({
      timer
    })
  },
  buySeckill:function(time){
    if(time<=0 || !time)return;
    let timerSeckill=this.data.timerSeckill;
    let seckillTime='';
    timerSeckill=setInterval(()=>{
      console.log(this.data.seckillTime)
      if(time<=0){
        clearInterval(this.data.timerSeckill);
        this.getIndex();
      }else if(!this.data.timerSeckill)clearInterval(this.data.timerSeckill);
      else time=time-1;
      seckillTime=this.timerCount(time);
      this.setData({
        seckillTime
      })
    },1000);
    this.setData({
      timerSeckill
    })
  },
  timerCount:function(hTime){
    // let d=Math.floor(time/86400)>9?Math.floor(time/86400):'0'+Math.floor(time/86400);
    // let hTime=time%86400;
    let h=Math.floor(hTime/3600)>9?Math.floor(hTime/3600):'0'+Math.floor(hTime/3600);
    let mTime=hTime%3600;
    let m=Math.floor(mTime/60)>9?Math.floor(mTime/60):'0'+Math.floor(mTime/60);
    let s=mTime%60>9?mTime%60:'0'+mTime%60;
    return {h,m,s}
  },
  getList () {
    const data = {
      isrecommend: 1,
      p: this.data.page
    }
    app.requestJs("Goods/goodsList",{data: data, status1: true, message: false}).then(({ data }) => {
      this.setData({
        list: [...this.data.list, ...data],
        page: ++this.data.page
      })
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if(options.scene){
      let invitecode=decodeURIComponent(options.scene);
      wx.setStorageSync('invitecode',invitecode);//邀请码
    }
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getIndex();
    this.getList()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    clearInterval(this.data.timer);
    clearInterval(this.data.timerSeckill);
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    clearInterval(this.data.timer);
    clearInterval(this.data.timerSeckill);
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    // this.data.page = this.data.page + 1
    this.getList()
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    
  }
})